﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sherwood.Content.UpdateScheduler;

namespace Sherwood.Samples.ArticleMeta
{
    public class ArticleSource : IRecordPullSource
    {
        public IEnumerable<string> GetAllRecords(UpdateContext context)
        {
            for (int i = 0; i < 10; i++)
            {
                string xml = null;
                try
                {
                    if (i == 4)
                        throw new InvalidOperationException("Could not read data for record 4");

                    xml = "<record xmlns=\"Sherwood.Content.Contracts\" referenceId=\"" + i + "\" />";
                }
                catch (Exception e)
                {
                    context.OnHandleException(this, new UpdateExceptionEventArgs(i.ToString(), e));
                }
                if (xml != null)
                    yield return xml;
            }
        }

        public string GetRecordByReferenceId(UpdateContext context, string referenceId)
        {
            return "<record xmlns=\"Sherwood.Content.Contracts\" referenceId=\"" + referenceId + "\" />";
        }

        public IEnumerable<string> GetRecordsModifiedSince(UpdateContext context, DateTime firstModificationDateUtc)
        {
            return GetAllRecords(context);
        }

        public IEnumerable<IRecord> GetDeletedRecords(IEnumerable<IRecord> referenceIds)
        {
            throw new NotImplementedException();
        }
    }
}
